import express from 'express';
import { MOCK_SERVER } from '../../constant/const';
import { trimStartStr } from '../../utils/util';

const router = express.Router();
const mockApis = require('../../mock');

const mountApi = (apis: Record<string, Function>) => {
  Object.entries(apis).forEach((func) => {
    const apiArr = func[0].split(' ');
    if (apiArr.length < 2) {
      return;
    }
    const apiName = apiArr[0].toLowerCase();
    const apiFunc = func[1];
    if (typeof apiFunc !== 'function') {
      return;
    }
    router[apiName]?.(trimStartStr(apiArr[1], MOCK_SERVER), (req, res) => {
      res.setHeader('Content-Type', 'application/json');
      return apiFunc(req, res);
    });
  });
};
Object.values(mockApis).forEach((apiObj) => {
  mountApi(apiObj as Record<string, Function>);
});

module.exports = router;
